package com.stox.ui.javafx.list.client.http;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import com.stox.client.http.AbstractHttpClient;
import com.stox.dto.Scrip;
import com.stox.dto.WatchList;
import com.stox.dto.exception.StoxException;
import com.stox.dto.web.Response;
import com.stox.ui.javafx.list.client.WatchListClient;

@Component
public class WatchListHttpClient extends AbstractHttpClient implements WatchListClient {
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Override
	public List<WatchList> findByParentId(Long id) {
		return extract(getRestTemplate().exchange(getRootUrl()+(id == null ? ("/watchLists") : ("/watchLists?parentId="+String.valueOf(id))), HttpMethod.GET, null, new ParameterizedTypeReference<Response<List<WatchList>>>(){}));
	}
	
	@Override
	public List<Scrip> findEntriesByWatchListId(Long id){
		if(null == id) return Collections.emptyList();
		return extract(getRestTemplate().exchange(getRootUrl() + "/watchLists/"+id+"/entries", HttpMethod.GET, null, new ParameterizedTypeReference<Response<List<Scrip>>>(){}));
	}
	
	@Override
	public void addToWatchList(List<Long> entryIds, Long watchListId) {
		if(null == watchListId)
			throw new StoxException();
		extract(getRestTemplate().exchange(getRootUrl() + "/watchLists/entries/"+watchListId, HttpMethod.POST, new HttpEntity<List<Long>>(entryIds), new ParameterizedTypeReference<Response<Void>>(){}));
	}
	
	@Override
	public void moveEntry(Long id, Long oldWatchListId, Long newWatchListId) {
		extract(getRestTemplate().exchange(getRootUrl()+"/watchLists/"+id+"?oldWatchListId="+oldWatchListId+"&newWatchListId="+newWatchListId, 
				HttpMethod.GET, null, new ParameterizedTypeReference<Response<Void>>() {}));
	}
	
	@Override
	public void rename(Long id, String newName) {
		extract(getRestTemplate().exchange(getRootUrl()+"/watchLists/"+id+"?name="+newName, HttpMethod.GET, null, new ParameterizedTypeReference<Response<Void>>(){}));
	}
	
	@Override
	public Long save(WatchList watchList) {
		return extract(getRestTemplate().exchange(getRootUrl() + "/watchLists",HttpMethod.POST, new HttpEntity<WatchList>(watchList), new ParameterizedTypeReference<Response<Long>>(){}));
	}
	
	@Override
	public void deleteWatchList(Long id) {
		extract(getRestTemplate().exchange(getRootUrl()+"/watchLists/"+String.valueOf(id), HttpMethod.DELETE, null, new ParameterizedTypeReference<Response<Void>>(){}));
	}
	
	@Override
	public void deleteWatchListEntry(Long entryId, Long watchListId) {
		extract(getRestTemplate().exchange(getRootUrl()+"/watchLists/"+watchListId+"/entries/"+entryId, HttpMethod.DELETE, null, new ParameterizedTypeReference<Response<Void>>(){}));
	}

	@Override
	public void deleteByParentId(Long parentId) {
		extract(getRestTemplate().exchange(getRootUrl() + "/watchLists/"+parentId+"/children", HttpMethod.DELETE, null, new ParameterizedTypeReference<Response<Void>>(){}));
	}

	@Override
	public void deleteEntriesByParentId(Long parentId) {
		extract(getRestTemplate().exchange(getRootUrl() + "/watchLists/"+parentId+"/entries", HttpMethod.DELETE, null, new ParameterizedTypeReference<Response<Void>>(){}));
	}

	@Override
	public void clear(Long id) {
		extract(getRestTemplate().exchange(getRootUrl() + "/watchLists/"+id+"/everything", HttpMethod.DELETE, null, new ParameterizedTypeReference<Response<Void>>(){}));
	}

}
